parent.window.EDITOR = {
	_info: {
		author: 'ZFSN',
		version: '1.0'
	},
	_data: {},
	_base: {
		header_frame: parent.window.frames['header'], // 菜单栏页面对象
		navigation_frame: parent.window.frames['navigation'], // 文件导航页面对象
		body_frame: parent.window.frames['body'] // 编辑器页面对象
	},
	_init_navigation: function() {
		$.fn.zTree.init($("#treeDemo"), navigationSetting, zNodes);
		var tree = $("#treeDemo");
		tree.css({
			'margin-top': '0px',
			'border': '0',
			'width': '97%',
			'height': '97%',
			'overflow-y': 'auto',
			'position': 'fixed'
		});
	},
	_init_editor: function(editorId, ace, editorSetting) {
		var header = this._base.header_frame;
		var langTools = ace.require("ace/ext/language_tools");
		// 初始化ide
		var editor = ace.edit(editorId);
		// 设置编程语言
		editor.getSession().setMode("ace/mode/" + editorSetting.language);
		// 设置主题
		editor.setTheme("ace/theme/" + editorSetting.theme);
		// 设置字体大小
		editor.setFontSize(editorSetting.fontSize);
		// 设置滚动条滚动速度
		editor.setScrollSpeed(editorSetting.scrollSpeed);
		// 设置是否只读
		editor.setOptions({readOnly: editorSetting.readOnly});
		// 设置是否显示每行文本长度辅助线
		editor.setShowPrintMargin(editorSetting.showPrintMargin);
		// 设置每行文本长度辅助线的位置
		editor.setPrintMarginColumn(editorSetting.printMarginColumn);
		
		editor.setOptions({
		    // 设置字体
		    fontFamily: editorSetting.fontFamily,
		    // 开启自动补全和语法提示功能
		    enableBasicAutocompletion: true,
		    enableSnippets: true,
		    enableLiveAutocompletion: true,
		    // 设置超距
		    scrollPastEnd: editorSetting.scrollPastEnd,
		    // 设置是否显示行号
		    showLineNumbers: editorSetting.showLineNumbers,
		    // 键盘事件
		    keyboardHandler: function(editor, a, b, c, keyEvent) {
		    	if (!keyEvent) return;
		    	if (keyEvent.altKey && keyEvent.key == 'v') {
		    		header.toggleFullScreen();
		    	}
		    }
		});
		// 新增语法提示
		langTools.addCompleter({
		    getCompletions: function(editor, session, pos, prefix, callback) {
		        if (prefix.length === 0) { callback(null, []); return }
		        callback(null, [
		        	{
			            name: 'document', //显示的名称，‘奖金’
			            value: 'getElementBySql', //插入的值，‘100’
			            score: 1000, //分数，越大的排在越上面
			            meta: '通过sql语句获取文档对象' //描述，‘我的常量’
		        	}
		        ]);
		    }
		});
		return editor;
	},
	// 保存
	save: function() {
		var body = this._base.body_frame;
		if (body.editor == undefined) return;
		var content = body.editor.getValue();
		console.log(content);
	},
	// 对比
	compare: function() {
		var body = this._base.body_frame;
		var random = Math.random() + '';
		var id = 'compareFile_' + random.substring(2);
		$('body').append('<input id="' + id + '" hidden="hidden" type="file" value="" />');
		
		$('#' + id).change(function() {
			// 获取选择的文件对象
			var selectedFile = document.getElementById(id).files[0];
			var fileName = selectedFile.name;
			var fileSize = selectedFile.size;
			
			// 校验文件类型和大小
			if (fileSize > 5242880) return;
			var index = fileName.lastIndexOf('.');
			if (index == -1) return;
			var suffixArr = ['.txt', '.java', '.js', '.html', '.css'];
			var suffix = fileName.substring(index);
			if (suffixArr.indexOf(suffix) == -1) return;
			
			// 校验是否存在当前打开的文件
			if (body.editor == undefined) return;
			
			// 读取文件
			var reader = new FileReader();
			reader.readAsText(selectedFile);
			
			// 读取完成后
			reader.onload = function() {
				body.readFileContent = this.result;
				var compareIndex = body.layer.open({
					type: 2,
					title: "文件比较结果",
					area: ['500px', '400px'],
					shadeClose: true,
					shift: 2,
					closeBtn: 1,
					content: 'compare.html',
					end: function() {
						body.readFileContent = '';
					}
				});
				// 打开既全屏
				layer.full(compareIndex);
			}
			
			$('#' + id).remove();
		});
		
		$('#' + id).click();
	},
	// 读取
	read: function() {
		var body = this._base.body_frame;
		var random = Math.random() + '';
		var id = 'readFile_' + random.substring(2);
		$('body').append('<input id="' + id + '" hidden="hidden" type="file" value="" />');
		
		$('#' + id).change(function() {
			// 获取选择的文件对象
			var selectedFile = document.getElementById(id).files[0];
			var fileName = selectedFile.name;
			var fileSize = selectedFile.size;
			
			// 校验文件类型和大小
			if (fileSize > 5242880) return;
			var index = fileName.lastIndexOf('.');
			if (index == -1) return;
			var suffixArr = ['.txt', '.java', '.js', '.html', '.css'];
			var suffix = fileName.substring(index);
			if (suffixArr.indexOf(suffix) == -1) return;
			
			// 读取文件
			var reader = new FileReader();
			reader.readAsText(selectedFile);
			
			// 读取完成后
			reader.onload = function() {
				body.active.tabAdd(fileName, id, this.result);
			}
			
			$('#' + id).remove();
		});
		
		$('#' + id).click();
	},
	// 设置
	setting: function() {
		var body = this._base.body_frame;
		body.layer.open({
			type: 2,
			title: "设置",
			area: ['500px', '400px'],
			shadeClose: true,
			shift: 2,
			closeBtn: 2,
			content: 'setting.html'
		});
	},
	// 关于
	about: function() {
		var body = this._base.body_frame;
		body.layer.open({
			type: 2,
			title: "关于",
			area: ['500px', '400px'],
			shadeClose: true,
			shift: 2,
			closeBtn: 2,
			content: 'about.html'
		});
	}
}
